package hz.mobileproject.pda.callroll.presenter;


import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.callroll.activity.CallrollActivity;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class PCalroll extends XPresent<CallrollActivity> {

    private String token;
    private final Context context;
    private Gson gson;
    private Request_data data;
    private String body_data;
    private RequestBody body;
    private String mrequest_data;

    public PCalroll(Context context) {
        gson = new Gson();
        data = new Request_data();
       this.context = context;
    }

    public void getData(String request_data,int type){
        mrequest_data=request_data;
        data.setData(Base64.getBase64(request_data));
        body_data=  gson.toJson(data);
        body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);
        token = context.getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
        getDataBody(body,type);

    }

    private void getDataBody(RequestBody body,int type) {
        switch (type){//  2.1.1	民警基本信息获取
            case 1:
                Api.getGankService().getloadPoliceInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                            }
                            @Override
                            public void onNext(Response_data gankResults) {

                                LogUtils.e("民警基本信息获取"+Base64.getFromBase64(gankResults.getData()));
                                getV().respnose_data(Base64.getFromBase64(gankResults.getData()),1);
                            }
                        });
                break;
            case 2://2.1.2	分监区查询
                Api.getGankService().getloadSubPrison(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                            }

                            @Override
                            public void onNext(Response_data data) {
                                LogUtils.e("分监区查询"+Base64.getFromBase64(data.getData()));
                                getV().respnose_data(Base64.getFromBase64(data.getData()),2);
                            }
                        });

                break;
            case 3://2.1.3	根据部门编号获取需要点名的罪犯列表
                Api.getGankService().getloadPrisonerList(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                            }

                            @Override
                            public void onNext(Response_data data) {
//                                getV().respnose_data(gson.toJson(data),"分监区查询");
                                Base64.getFromBase64(data.getData());
                                if (data.getData().equals("W10=")){

                                }else {
                                    getV().respnose_data(Base64.getFromBase64(data.getData()),3);
                                }
                            }

                        });
                break;
            case 4://  2.1.4  获取点名批次配置信息
                Api.getGankService().getloadCnf(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                            }

                            @Override
                            public void onNext(Response_data gankResults) {
                                if (!gankResults.getRtnCode().equals("1")){
                                    LogUtils.e("获取点名批次配置信息"+Base64.getFromBase64(gankResults.getData()));
                                    String json=Base64.getFromBase64(gankResults.getData());
                                    getV().respnose_data(json,4);
                                }else {
                                    getV().respnose_data("不在点名时间",4);
                                }
                            }
                        });
                break;
            case 6://2.1.6	开始点名
                Api.getGankService().getstart(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                            }

                            @Override
                            public void onNext(Response_data gankResults) {

                                LogUtils.e("开始点名"+Base64.getFromBase64(gankResults.getData()));
                                //{"call_total_num":209,"flowout_num":0,"inflow_num":0,"inpriosn_num":209,"recode_id":101515}
                               if(gankResults.getRtnCode().equals("1")){   //已经点过名
                                   getV().respnose_data("已经点过名",6);
                               }else if (gankResults.getRtnCode().equals("0")){
                                   String json= Base64.getFromBase64(gankResults.getData());
                                   LogUtils.e("开始点名"+json);
                                   getV().respnose_data(json,6);
                               }else {
                                   LogUtils.e("开始点名 启动失败 错误码：" + gankResults.getRtnCode());
                               }
                            }
                        });
                break;

            case 7://2.1.7		点名数据
                Api.getGankService().getcalling(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mrequest_data,7);
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                             /*   LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().respnose_data(json,);


*/

                             getV().respnose_data(body_data,7);
                            }
                        });
                break;
            case 8://2.1.8	点名结束
                Api.getGankService().getend(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                                getV().Error(error,mrequest_data,8);
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().respnose_data(json,8);
                            }
                        });
                break;
            case 9://2.1.8	上传离线点名结束
                Api.getGankService().getend(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
                                getV().Error(error,mrequest_data,8);
                            }

                            @Override
                            public void onNext(Response_data gankResults) {

                            }
                        });
                break;


            case 10://2.1.10 获取类型
                Api.getGankService().getloadtype(token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                getV().respnose_data(Base64.getFromBase64(gankResults.getData()),10);
                            }
                        });
                break;

            case 11://2.1.10 插入流动
                Api.getGankService().addTempFlow(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
//                                getV().Error(error,mrequest_data,8);
                            }

                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                if (gankResults.getRtnCode().equals("0")){

                                    try {
                                        JSONObject jsonObject = new JSONObject(mrequest_data);
                                      String prisonid=  jsonObject.getString("prisonerId");

                                        getV().respnose_data(prisonid,11);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }



                                }
//                                getV().respnose_data(Base64.getFromBase64(gankResults.getData()),10);
                            }
                        });
                break;


            case 12://2.1.10 获取部门
                Api.getGankService().getloadDept(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
//                        getV().showError(error);
//                                getV().Error(error,mrequest_data,8);
                            }

                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                getV().respnose_data(Base64.getFromBase64(gankResults.getData()),12);
                            }
                        });
                break;
            case 13:    //请求数据库中没有提交的值
                Api.getGankService().getcalling(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mrequest_data,7);
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                             /*   LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().respnose_data(json,);


*/

                                getV().respnose_data(body_data,13);
                            }
                        });
                break;
            case 55:
                Api.getGankService().backToPrison(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mrequest_data,55);
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getRtnCode()));

                                getV().respnose_data(Base64.getFromBase64(gankResults.getData()),55);
                            }
                        });
                break;


        }


    }

}

